package com.wwh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wwh.mapper.PostMapper;
import com.wwh.mapper.RelationMapper;
import com.wwh.model.Post;
import com.wwh.model.Relation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PostController {
    @Autowired
    PostMapper postMapper;
    @Autowired
    RelationMapper relationMapper;

    @RequestMapping("/createPost")
    public String createPost(){
        return "post/create";
    }

    @RequestMapping("/addPost")
    public String listPost(Post p, HttpServletRequest request) throws Exception {
        int save = postMapper.save(p);
        int userId = (int) request.getSession().getAttribute("userId");
        Relation relation = new Relation();
        relation.user_id = userId;
        relation.post_id = p.getId();
        relationMapper.save(relation);
        return "redirect:listPostBack";
    }
    @RequestMapping("/deletePost")
    public String deletePost(Post p) throws Exception {
        postMapper.delete(p.getId());
        return "redirect:listPostBack";
    }
    @RequestMapping("/updatePost")
    public String updatePost(Post p) throws Exception {
        postMapper.update(p);
        return "redirect:listPostBack";
    }

    /**
     * 只能编辑登陆者自己的文章，
     * 如果文章id不属于自己，则只能查看。
     * @param id
     * @param m
     * @return
     * @throws Exception
     */
    @RequestMapping("/editPost")
    public String listPost(int id, Model m, HttpServletRequest request) throws Exception {
        int userId = (int) request.getSession().getAttribute("userId");
        Relation relation = relationMapper.search(userId, id);
        if(relation != null){
            Post p= postMapper.get(id);
            m.addAttribute("c", p);
            return "post/edit";
        }

        return "redirect:detailView?id="+id;
    }

    /**
     * 默认首页，可以展示所有文章
     * @param m
     * @param start
     * @param size
     * @return
     * @throws Exception
     */
    @RequestMapping("/listPostFront")
    public String listPostFront(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size, HttpServletRequest request) throws Exception {
        PageHelper.startPage(start,size,"id desc");
        List<Post> cs=postMapper.findAll();
        PageInfo<Post> page = new PageInfo<>(cs);
        Object userInfo = request.getSession().getAttribute("userInfo");
        m.addAttribute("page", page);
        if(userInfo != null){
            return "post/list_frontend_pure";
        }
        return "post/list_frontend";
    }

    /**
     * 注册或者登录成功后的后台界面，
     * 只展示登陆者自己的文章。
     * @param m
     * @param start
     * @param size
     * @return
     * @throws Exception
     */
    @RequestMapping("/listPostBack")
    public String listPostBackend(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size, HttpServletRequest request) throws Exception {
        PageHelper.startPage(start,size,"id desc");
        int userId = (int) request.getSession().getAttribute("userId");
        List<Post> cs=postMapper.findSelfPost(userId);
        PageInfo<Post> page = new PageInfo<>(cs);
        m.addAttribute("page", page);
        return "post/list_backend";
    }


    @RequestMapping("/detailPost")
    public String detailPost(int id, Model m) throws Exception{
        Post p = postMapper.get(id);
        m.addAttribute("c", p);
        return "post/detail_edit";
    }

    @RequestMapping("/detailView")
    public String detailView(int id, Model m) throws Exception{
        Post p = postMapper.get(id);
        m.addAttribute("c", p);
        return "post/detail_view";
    }

}
